package com.ecommerce;
import com.ecommerce.dao.DatabaseConnection;
import com.ecommerce.model.User;
import com.ecommerce.service.UserService;
import com.ecommerce.service.ProductService;
import com.ecommerce.service.CartService;
import com.ecommerce.service.OrderService;
import com.ecommerce.service.InventoryService;
import com.ecommerce.service.SalesService;
import com.ecommerce.service.CouponService;
import com.ecommerce.util.InputUtil;

public class Main {
    private UserService userService;
    private ProductService productService;
    private User currentUser;
    private CartService cartService;
    private OrderService orderService;
    private InventoryService inventoryService;
    private SalesService salesService;
    private CouponService couponService;

    public Main() {
        this.userService = new UserService();
        this.productService = new ProductService();
        this.cartService = new CartService();
        this.orderService = new OrderService();
        this.inventoryService = new InventoryService();
        this.salesService = new SalesService();
        this.couponService = new CouponService();
        this.currentUser = null;
    }

    public void start() {
        System.out.println("=== 欢迎使用电子商务系统 ===");

        // 测试数据库连接
        testDatabaseConnection();

        while (true) {
            if (currentUser == null) {
                showMainMenu();
            } else {
                if (userService.isAdmin(currentUser)) {
                    showAdminMenu();
                } else {
                    showCustomerMenu();
                }
            }
        }
    }

    private void testDatabaseConnection() {
        System.out.println("正在连接数据库...");
        try {
            DatabaseConnection.getConnection();
            System.out.println("数据库连接成功！");
        } catch (Exception e) {
            System.out.println("数据库连接失败: " + e.getMessage());
            System.out.println("请检查：");
            System.out.println("1. MySQL服务是否启动");
            System.out.println("2. 数据库配置是否正确");
            System.out.println("3. ecommerce_system数据库是否存在");
            System.out.println("\n系统将继续运行，但部分功能可能无法使用。");
        }
    }

    private void showMainMenu() {
        System.out.println("\n=== 主菜单 ===");
        System.out.println("1. 用户注册");
        System.out.println("2. 用户登录");
        System.out.println("3. 退出系统");

        int choice = InputUtil.getIntInput("请选择操作: ");

        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                exit();
                break;
            default:
                System.out.println("无效选择，请重新输入！");
        }
    }

    private void showAdminMenu() {
        System.out.println("\n=== 管理员菜单 ===");
        System.out.println("1. 用户管理");
        System.out.println("2. 商品管理");
        System.out.println("3. 库存管理");
        System.out.println("4. 订单管理");
        System.out.println("5. 销售统计");
        System.out.println("6. 优惠券管理");
        System.out.println("7. 查看我的信息");
        System.out.println("8. 退出登录");

        int choice = InputUtil.getIntInput("请选择操作: ");

        switch (choice) {
            case 1:
                userService.manageUsers(currentUser);
                break;
            case 2:
                productService.manageProducts();
                break;
            case 3:
                inventoryService.manageInventory(currentUser.getId());
                break;
            case 4:
                orderService.manageAdminOrders();
                break;
            case 5:
                salesService.manageSales();
                break;
            case 6:
                couponService.manageCoupons();
                break;
            case 7:
                userService.displayMyInfo(currentUser);
                break;
            case 8:
                logout();
                break;
            default:
                System.out.println("无效选择，请重新输入！");
        }
    }

    private void showCustomerMenu() {
        System.out.println("\n=== 顾客菜单 ===");
        System.out.println("1. 浏览商品");
        System.out.println("2. 购物车管理");
        System.out.println("3. 订单管理");
        System.out.println("4. 查看我的信息");
        System.out.println("5. 退出登录");

        int choice = InputUtil.getIntInput("请选择操作: ");

        switch (choice) {
            case 1:
                productService.browseProductsForCustomerMenu();
                break;
            case 2:
                cartService.manageCart(currentUser.getId());
                break;
            case 3:
                orderService.manageCustomerOrders(currentUser.getId());
                break;
            case 4:
                userService.displayMyInfo(currentUser);
                break;
            case 5:
                logout();
                break;
            default:
                System.out.println("无效选择，请重新输入！");
        }
    }

    private void register() {
        System.out.println("\n=== 用户注册 ===");
        String username = InputUtil.getStringInput("请输入用户名: ");
        String password = InputUtil.getStringInput("请输入密码: ");
        String email = InputUtil.getStringInput("请输入邮箱: ");

        userService.register(username, password, email);
    }

    private void login() {
        System.out.println("\n=== 用户登录 ===");
        String username = InputUtil.getStringInput("请输入用户名: ");
        String password = InputUtil.getStringInput("请输入密码: ");

        currentUser = userService.login(username, password);
    }

    private void logout() {
        System.out.println("再见，" + currentUser.getUsername() + "！");
        currentUser = null;
    }

    private void exit() {
        System.out.println("感谢使用电子商务系统，再见！");
        System.exit(0);
    }

    public static void main(String[] args) {
        new Main().start();
    }
}